package com.erp.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.user.domain.ErpBranchOfficeLimit;
import com.erp.user.domain.bo.ErpBranchOfficeLimitBo;
import com.erp.user.domain.vo.ErpBranchOfficeLimitVo;
import com.erp.user.mapper.ErpBranchOfficeLimitMapper;
import com.erp.user.service.IErpBranchOfficeLimitService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 分公司开户额度Service业务层处理
 *
 * @author erp
 * @date 2023-05-17
 */
@RequiredArgsConstructor
@Service
public class ErpBranchOfficeLimitServiceImpl implements IErpBranchOfficeLimitService {

    private final ErpBranchOfficeLimitMapper baseMapper;

    /**
     * 查询分公司开户额度
     */
    @Override
    public ErpBranchOfficeLimitVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询分公司开户额度列表
     */
    @Override
    public TableDataInfo<ErpBranchOfficeLimitVo> queryPageList(ErpBranchOfficeLimitBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpBranchOfficeLimit> lqw = buildQueryWrapper(bo);
        Page<ErpBranchOfficeLimitVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询分公司开户额度列表
     */
    @Override
    public List<ErpBranchOfficeLimitVo> queryList(ErpBranchOfficeLimitBo bo) {
        LambdaQueryWrapper<ErpBranchOfficeLimit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpBranchOfficeLimit> buildQueryWrapper(ErpBranchOfficeLimitBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpBranchOfficeLimit> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBranchId() != null, ErpBranchOfficeLimit::getBranchId, bo.getBranchId());
        lqw.eq(bo.getAgentNum() != null, ErpBranchOfficeLimit::getAgentNum, bo.getAgentNum());
        lqw.eq(bo.getNotAgentNum() != null, ErpBranchOfficeLimit::getNotAgentNum, bo.getNotAgentNum());
        lqw.eq(bo.getUserNum() != null, ErpBranchOfficeLimit::getUserNum, bo.getUserNum());
        lqw.eq(bo.getNotUserNum() != null, ErpBranchOfficeLimit::getNotUserNum, bo.getNotUserNum());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpBranchOfficeLimit::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增分公司开户额度
     */
    @Override
    public Boolean insertByBo(ErpBranchOfficeLimitBo bo) {
        ErpBranchOfficeLimit add = BeanUtil.toBean(bo, ErpBranchOfficeLimit.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改分公司开户额度
     */
    @Override
    public Boolean updateByBo(ErpBranchOfficeLimitBo bo) {
        ErpBranchOfficeLimit officeLimit = baseMapper.selectOne(new LambdaQueryWrapper<ErpBranchOfficeLimit>().eq(ErpBranchOfficeLimit::getBranchId, bo.getBranchId()));
        if (officeLimit == null) {
            ErpBranchOfficeLimit branchOfficeLimit = new ErpBranchOfficeLimit();
            branchOfficeLimit.setBranchId(bo.getBranchId());
            baseMapper.insert(branchOfficeLimit);
            officeLimit = baseMapper.selectOne(new LambdaQueryWrapper<ErpBranchOfficeLimit>().eq(ErpBranchOfficeLimit::getBranchId, bo.getBranchId()));
        }
        officeLimit.setAgentNum(bo.getAgentNum() + officeLimit.getAgentNum());
        officeLimit.setNotAgentNum(bo.getNotAgentNum() + officeLimit.getNotAgentNum());
        officeLimit.setUserNum(bo.getUserNum() + officeLimit.getUserNum());
        officeLimit.setNotUserNum(bo.getNotUserNum() + officeLimit.getNotUserNum());

        return baseMapper.updateById(officeLimit) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpBranchOfficeLimit entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除分公司开户额度
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ErpBranchOfficeLimit selectLimitByBranchId(Long branchId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<ErpBranchOfficeLimit>().eq(ErpBranchOfficeLimit::getBranchId,branchId));
    }


}
